package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjCodeFieldContent;
import com.zb.mapper.ZjCodeFieldContentMapper;
import com.zb.service.ZjCodeFieldContentService;
import com.zb.vo.CodeFieldContentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 编码字段内容表，当category=2/3时，内容存入该表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-23
 */
@Service
public class ZjCodeFieldContentServiceImpl extends ServiceImpl<ZjCodeFieldContentMapper, ZjCodeFieldContent> implements ZjCodeFieldContentService {
    @Resource
    private ZjCodeFieldContentMapper fieldContentMapper;

    // 新增、修改、删除字段内容
    @Override
    public void save(Long custAccId, Long codeFieldId, List<CodeFieldContentVo> contentVos, List<Long> delContentVoIds) {
        if (null != delContentVoIds && !delContentVoIds.isEmpty()) {
            this.removeByIds(delContentVoIds);
        }
        if (null == contentVos || contentVos.isEmpty()) {
            return;
        }
        List<ZjCodeFieldContent> adds = contentVos.stream().filter(vo -> null == vo.getId()).map(vo -> {
            ZjCodeFieldContent content = new ZjCodeFieldContent();
            content.setId(IdWorker.getId());
            content.setCustAccId(custAccId);
            content.setCodeFieldId(codeFieldId);
            content.setVal(vo.getVal());
            return content;
        }).toList();
        List<ZjCodeFieldContent> updates = contentVos.stream().filter(vo -> null != vo.getId()).map(vo -> {
            ZjCodeFieldContent content = new ZjCodeFieldContent();
            BeanUtils.copyProperties(vo, content);
            return content;
        }).toList();
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
    }

    // 根据编码字段ID集合查询字段内容
    @Override
    public List<ZjCodeFieldContent> getByCodeFieldIds(List<Long> codeFieldIds) {
        if (null == codeFieldIds || codeFieldIds.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjCodeFieldContent> wrapper = new QueryWrapper<>();
        wrapper.select("id", "code_field_id", "val");
        wrapper.in("code_field_id", codeFieldIds);
        return this.list(wrapper);
    }



    // 根据编码字段ID集合查询字段内容
    @Override
    public List<ZjCodeFieldContent> getByCodeFieldId(Long codeFieldId) {
        QueryWrapper<ZjCodeFieldContent> wrapper = new QueryWrapper<>();
        wrapper.select("id", "code_field_id", "val");
        wrapper.eq("code_field_id", codeFieldId);
        return this.list(wrapper);
    }

    // 根据编码字段ID删除
    @Override
    public void deleteByCodeFields(List<Long> codeFieldIds) {
        if (null == codeFieldIds || codeFieldIds.isEmpty()) {
            return;
        }
        fieldContentMapper.deleteByCodeFields(codeFieldIds);
    }
}
